package com.jhc.drone.communication.network.router;

import com.jhc.drone.communication.properties.ProtocolProperties;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

public class TcpRouter {

    public static void start(ProtocolProperties protocolProperties) {
        try {
            final Integer routerGoalPort = protocolProperties.routerGoalPort;
            final Integer tcpServerPort = protocolProperties.port;
            final Integer qgcPort = protocolProperties.qgcPort;
            final Integer routerLocalPort = protocolProperties.routerLocalPort;
            final String qgcAddress = protocolProperties.qgcAddress;
            final String serverAddress = protocolProperties.serverAddress;

            final DatagramSocket server1Socket = new DatagramSocket(routerGoalPort);
            final DatagramSocket serverSocket9999 = new DatagramSocket();
            final DatagramSocket serverSocket8600 = new DatagramSocket();
            // TCP Server socket for 7901 (serverSocket7901)
            InetAddress address = InetAddress.getByName(qgcAddress);
            byte[] receiveData = new byte[1024];
            byte[] receiveData9999 = new byte[1024];
            Socket tcpSocket = new Socket(serverAddress, tcpServerPort);
            new Thread(() -> {
                while (true) {
                    try {
                        // 1. 接收来自client1和client2的消息
                        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                        server1Socket.receive(receivePacket);
                        String receivedMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
                        System.out.println("Received message from client: " + receivedMessage);

                        // 2. 转发消息给client2
                        DatagramPacket forwardToClient2 = new DatagramPacket(receivePacket.getData(), receivePacket.getLength(), address, qgcPort);
                        serverSocket9999.send(forwardToClient2);
                        System.out.println("Forwarded message to client2.");

                        // 3. 转发消息给TCP Server (7901)
                        OutputStream tcpOutputStream = tcpSocket.getOutputStream();

                        tcpOutputStream.write(receivePacket.getData(), 0, receivePacket.getLength());
                        tcpOutputStream.flush();
                        System.out.println("Forwarded message to TCP Server at port 7901.");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            new Thread(() -> {
                try {
                    InputStream inputStream = tcpSocket.getInputStream();
                    byte[] buffer = new byte[1024];
                    while (true) {
                        // 读取来自 TCP Server 7901 的数据
                        int bytesRead = inputStream.read(buffer);
                        if (bytesRead == -1) {
                            break;
                        }
                        String server2Message = new String(buffer, 0, bytesRead);
                        System.out.println("Received message from TCP Server (7901): " + server2Message);

                        // 转发给 client1（UDP 端口 8600）
                        DatagramPacket forwardToClient1 = new DatagramPacket(buffer, bytesRead, address, routerLocalPort);
                        serverSocket8600.send(forwardToClient1);
                        System.out.println("Forwarded message from TCP Server to client1 (UDP 8600).");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();

            // 5. 接收来自client1和client2的消息并转发给client2
            new Thread(() -> {
                while (true) {
                    try {
                        DatagramPacket receivePacket = new DatagramPacket(receiveData9999, receiveData9999.length);
                        serverSocket9999.receive(receivePacket);
                        String receivedMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
                        System.out.println("Received message from client: " + receivedMessage);

                        // 转发消息给client2
                        DatagramPacket forwardToClient2 = new DatagramPacket(receivePacket.getData(), receivePacket.getLength(), address, routerLocalPort);
                        serverSocket8600.send(forwardToClient2);

                        System.out.println("Forwarded message to client2.");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
